package com.itheima.aspect;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

//演示AOP中的JoinPoint对象方法
@Slf4j
@Aspect
@Component
@Order(10)
public class AspectDemo5 {

    @Autowired
    HttpServletRequest request;

    //前置通知
    @Before("@annotation(com.itheima.aspect.MyLog)")
    public void m2(JoinPoint jp) {

        log.info("方法名:{}", jp.getSignature().getName());
        log.info("类名:{}", jp.getTarget().getClass());
        log.info("方法实际参数:{}", Arrays.toString(jp.getArgs()));
        log.info("方法的返回值类型:{}", jp);
    }

    //环绕通知
    @SneakyThrows
    @Around("@annotation(com.itheima.aspect.MyLog)")
    public Object m1(ProceedingJoinPoint pjp) {

        Object proceed = pjp.proceed();

        MethodSignature ms = (MethodSignature)pjp.getSignature();

        log.info("方法名:{}", pjp.getSignature().getName());
        log.info("类名:{}", pjp.getTarget().getClass());
        log.info("方法实际参数:{}", Arrays.toString(pjp.getArgs()));
        log.info("方法的返回值类型:{}", ms.getReturnType());

        return proceed;
    }

}
